home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch6 / Compose2.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-04-27  |  5.7 KB  |  174 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmCompose2 
  4.    Caption         =   "Compose2 []"
  5.    ClientHeight    =   6810
  6.    ClientLeft      =   165
  7.    ClientTop       =   735
  8.    ClientWidth     =   8610
  9.    LinkTopic       =   "Form2"
  10.    ScaleHeight     =   6810
  11.    ScaleWidth      =   8610
  12.    StartUpPosition =   3  'Windows Default
  13.    Begin MSComDlg.CommonDialog dlgOpenFile 
  14.       Left            =   0
  15.       Top             =   840
  16.       _ExtentX        =   847
  17.       _ExtentY        =   847
  18.       _Version        =   393216
  19.    End
  20.    Begin VB.PictureBox picBackground 
  21.       AutoSize        =   -1  'True
  22.       Height          =   3360
  23.       Left            =   120
  24.       Picture         =   "Compose2.frx":0000
  25.       ScaleHeight     =   220
  26.       ScaleMode       =   3  'Pixel
  27.       ScaleWidth      =   274
  28.       TabIndex        =   2
  29.       Top             =   0
  30.       Width           =   4170
  31.    End
  32.    Begin VB.PictureBox picForeground 
  33.       AutoSize        =   -1  'True
  34.       Height          =   3360
  35.       Left            =   4320
  36.       Picture         =   "Compose2.frx":2C462
  37.       ScaleHeight     =   220
  38.       ScaleMode       =   3  'Pixel
  39.       ScaleWidth      =   274
  40.       TabIndex        =   1
  41.       Top             =   0
  42.       Width           =   4170
  43.    End
  44.    Begin VB.PictureBox picResult 
  45.       Height          =   3360
  46.       Left            =   2220
  47.       ScaleHeight     =   220
  48.       ScaleMode       =   3  'Pixel
  49.       ScaleWidth      =   274
  50.       TabIndex        =   0
  51.       Top             =   3360
  52.       Width           =   4170
  53.    End
  54.    Begin VB.Menu mnuFile 
  55.       Caption         =   "&File"
  56.       Begin VB.Menu mnuFileSaveAs 
  57.          Caption         =   "Save &As..."
  58.          Shortcut        =   ^A
  59.       End
  60.    End
  61. Attribute VB_Name = "frmCompose2"
  62. Attribute VB_GlobalNameSpace = False
  63. Attribute VB_Creatable = False
  64. Attribute VB_PredeclaredId = True
  65. Attribute VB_Exposed = False
  66. Option Explicit
  67. ' Make a mask from the foreground picture.
  68. Private Sub ComposeImages()
  69. Dim background_pixels() As RGBTriplet
  70. Dim foreground_pixels() As RGBTriplet
  71. Dim bits_per_pixel As Integer
  72. Dim transparent_r As Byte
  73. Dim transparent_g As Byte
  74. Dim transparent_b As Byte
  75. Dim is_transparent As Boolean
  76. Dim X As Integer
  77. Dim Y As Integer
  78.     ' Get the pixels from the images.
  79.     GetBitmapPixels picBackground, background_pixels, bits_per_pixel
  80.     GetBitmapPixels picForeground, foreground_pixels, bits_per_pixel
  81.     ' See what the upper left pixel's color is.
  82.     ' We will convert this value into white and other
  83.     ' values into black.
  84.     With foreground_pixels(0, 0)
  85.         transparent_r = .rgbRed
  86.         transparent_g = .rgbGreen
  87.         transparent_b = .rgbBlue
  88.     End With
  89.     ' Set the result color values.
  90.     For Y = 0 To picForeground.ScaleHeight - 1
  91.         For X = 0 To picForeground.ScaleWidth - 1
  92.             With foreground_pixels(X, Y)
  93.                 If (.rgbRed = transparent_r) And _
  94.                    (.rgbGreen = transparent_g) And _
  95.                    (.rgbBlue = transparent_b) _
  96.                 Then
  97.                     ' Use the background color.
  98.                     foreground_pixels(X, Y) = background_pixels(X, Y)
  99.                 Else
  100.                     ' Leave the foreground color unchanged.
  101.                 End If
  102.             End With
  103.         Next X
  104.     Next Y
  105.     ' Set picResult's pixels.
  106.     SetBitmapPixels picResult, bits_per_pixel, foreground_pixels
  107.     picResult.Picture = picResult.Image
  108. End Sub
  109. ' Start in the current directory.
  110. Private Sub Form_Load()
  111. Dim ctl As Control
  112.     For Each ctl In Controls
  113.         If TypeOf ctl Is PictureBox Then
  114.             ctl.ScaleMode = vbPixels
  115.             ctl.AutoRedraw = True
  116.         End If
  117.     Next ctl
  118.     picBackground.AutoSize = True
  119.     picForeground.AutoSize = True
  120.     dlgOpenFile.CancelError = True
  121.     dlgOpenFile.InitDir = App.Path
  122.     dlgOpenFile.Filter = _
  123.         "Bitmaps (*.bmp)|*.bmp|" & _
  124.         "GIFs (*.gif)|*.gif|" & _
  125.         "JPEGs (*.jpg)|*.jpg;*.jpeg|" & _
  126.         "Icons (*.ico)|*.ico|" & _
  127.         "Cursors (*.cur)|*.cur|" & _
  128.         "Run-Length Encoded (*.rle)|*.rle|" & _
  129.         "Metafiles (*.wmf)|*.wmf|" & _
  130.         "Enhanced Metafiles (*.emf)|*.emf|" & _
  131.         "Graphic Files|*.bmp;*.gif;*.jpg;*.jpeg;*.ico;*.cur;*.rle;*.wmf;*.emf|" & _
  132.         "All Files (*.*)|*.*"
  133.     ' Make the form appear.
  134.     Show
  135.     Screen.MousePointer = vbHourglass
  136.     DoEvents
  137.     ' Compose the images.
  138.     ComposeImages
  139.     Screen.MousePointer = vbDefault
  140. End Sub
  141. ' Save the transformed image.
  142. Private Sub mnuFileSaveAs_Click()
  143. Dim file_name As String
  144.     ' Let the user select a file.
  145.     On Error Resume Next
  146.     dlgOpenFile.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  147.     dlgOpenFile.ShowSave
  148.     If Err.Number = cdlCancel Then
  149.         Exit Sub
  150.     ElseIf Err.Number <> 0 Then
  151.         Beep
  152.         MsgBox "Error selecting file.", , vbExclamation
  153.         Exit Sub
  154.     End If
  155.     On Error GoTo 0
  156.     Screen.MousePointer = vbHourglass
  157.     DoEvents
  158.     file_name = Trim$(dlgOpenFile.FileName)
  159.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  160.         - Len(dlgOpenFile.FileTitle) - 1)
  161.     Caption = "Compose [" & dlgOpenFile.FileTitle & "]"
  162.     ' Save the transformed image into the file.
  163.     On Error GoTo SaveError
  164.     SavePicture picResult.Picture, file_name
  165.     On Error GoTo 0
  166.     Screen.MousePointer = vbDefault
  167.     Exit Sub
  168. SaveError:
  169.     Screen.MousePointer = vbDefault
  170.     MsgBox "Error " & Format$(Err.Number) & _
  171.         " saving file '" & file_name & "'" & vbCrLf & _
  172.         Err.Description
  173. End Sub
  174.